﻿//----------------------------------------------------------------
// <copyright file="STCache.cs" >
//    Top Do ---TOP API DEMO 示例系列之
//    CLR Version: 2.0.50727.1434
//    NameSpace:  SpaceTime
//    FileClass: STCache - Cache数据缓存管理类
//
//    By 淘宝旺旺ID: 幻影时空
//    Email:visionspace@st-soft.cn
//    Copyright (C) SpaceTime SoftWare , All rights reserved (www.st-soft.cn).
//
//    项目引用 System.Net 类
// </copyright>
//----------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
namespace SpaceTime
{
    /// <summary>
    /// VsCache 的摘要说明
    /// </summary>
    public class STCache
    {
        public STCache()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }


        private static void onRemove(string strIdentify, object userInfo, CacheItemRemovedReason reason)
        {

        }
        //建立回调委托的一个实例
        CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);


        /// <summary>
        /// 创建缓存对象（默认24小时缓存）
        /// </summary>
        /// <param name="name">键名</param>
        /// <param name="value">键值</param>
        public static void CreateCache(string name, object value)
        {
            CreateCache(name, 24, value);
        }

        /// <summary>
        /// 创建缓存对象(指定缓存小时)
        /// </summary>
        /// <param name="name">键名</param>
        /// <param name="hours">缓存时间(小时)</param>
        /// <param name="value">键值</param>
        public static void CreateCache(string name,double hours, object value)
        {
            //建立回调委托的一个实例
            CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);

            //以Identify为标志，将userInfo存入Cache
            HttpContext.Current.Cache.Insert(name, value, null,
                 System.DateTime.Now.AddHours(hours),//当前指定为24小时,7*24=168
                 System.Web.Caching.Cache.NoSlidingExpiration,
                 System.Web.Caching.CacheItemPriority.Default,
                 callBack);
        }

        /// <summary>
        /// 读取缓存对象结果
        /// </summary>
        /// <param name="name">键名</param>
        /// <returns></returns>
        public static object GetCache(string name)
        {
            if (ExistCache(name))
            {
                return HttpContext.Current.Cache[name];
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 判断存储的"一键一值"值是否还存在（有没有过期失效或从来都未存储过）
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool ExistCache(string name)
        {
            if (HttpContext.Current.Cache[name] != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除缓存数据
        /// </summary>
        /// <param name="name"></param>
        public static void Remove(string name)
        {
            try
            {
                HttpContext.Current.Cache.Remove(name);                
            }
            catch (Exception exp)
            { 
                //
            }

        }
    }
}